﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using IceDog.UpdateDB.IRepository;
using IceDog.UpdateDB.Model;
using IceDog.UpdateDB.Model.SingleModel;
using IceDog.UpdateDB.Repository;
using Microsoft.AspNetCore.Mvc;

namespace IceDog.UpdateDB.WebServer.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class UsersController : ControllerBase
    {
        private readonly IUsersRepository _repository;
        public UsersController(IUsersRepository repository)
        {
            _repository = repository;
        }
        // GET api/values
        [HttpGet]
        public ActionResult<IEnumerable<UserModel>> Get()
        {
            return _repository.SelectAll();
        }

        // GET api/values/5
        [HttpGet("{id}")]
        public ActionResult<UserModel> Get(int id)
        {
            return _repository.SelectOne(id);
        }

        // POST api/values
        [HttpPost]
        public void Post([FromBody] UserModel model)
        {
            _repository.Add(model);
        }

        // PUT api/values/5
        [HttpPut("{id}")]
        public void Put(int id, [FromBody] UserModel model)
        {
            var user = _repository.SelectOne(id);
            user.Name = model.Name;
            _repository.Update(user);
        }

        // DELETE api/values/5
        [HttpDelete("{id}")]
        public void Delete(int id)
        {
            _repository.Delete(id);
        }
    }
}
